

//
//  HTTPURL.m
//  QiXingIos
//
//  Created by 李雪虎 on 2016/11/15.
//  Copyright © 2016年 王旭阳. All rights reserved.
//

#import "HTTPURL.h"

@implementation HTTPURL

+(void)postRequest:(NSString *)url parameters:(id)parameter success:(void (^)(NSURLSessionDataTask *  task, id   responseObject))ltSuccess
            filure:(void (^)(NSURLSessionDataTask *  task, id   error))ltFailure
{
    NSLog(@"%@~~~%@",url,parameter);
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        ltSuccess(task,responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        ltFailure(task,error);
    }];
}
+(void)postRequest5s:(NSString *)url parameters:(id)parameter success:(void (^)(NSURLSessionDataTask *  task, id   responseObject))ltSuccess
            filure:(void (^)(NSURLSessionDataTask *  task, id   error))ltFailure
{
    NSLog(@"%@~~~%@",url,parameter);
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    config.timeoutIntervalForRequest = 5.0;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        ltSuccess(task,responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        ltFailure(task,error);
    }];
}

+(void)getRequest:(NSString *)url parameters:(id)parameter success:(void (^)(NSURLSessionDataTask *  task, id   responseObject))ltSuccess
           filure:(void (^)(NSURLSessionDataTask *  task, id   error))ltFailure
{
    NSLog(@"%@~~~%@",url,parameter);
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        ltSuccess(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        ltFailure(task,error);
    }];
}
//提示框(一个按钮的)
+(UIAlertController *)showTwoAlertWithTitle:(NSString *)title
                                    message:( NSString *)message
                             preferredStyle:(UIAlertControllerStyle)preferredStyle
                                  sureTitle:(NSString *)sureTitle
                                cancelTitle:(NSString *)cancelTitle
                                  sureBlock:(void(^)())sureBlock
                                cancelBlock:(void(^)())cancelBlock
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    [alert addAction:[UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (sureBlock) {
            sureBlock();
        }
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        cancelBlock();
    }]];
    return alert;
}
//提示框(一个按钮的)
+(UIAlertController *)showOneAlertWithTitle:(NSString *)title
                                    message:( NSString *)message
                             preferredStyle:(UIAlertControllerStyle)preferredStyle
                                  sureTitle:(NSString *)sureTitle
                                  sureBlock:(void(^)())sureBlock
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    [alert addAction:[UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (sureBlock) {
            sureBlock();
        }
    }]];
    return alert;
}

+(void)MJheaderDH:(MJRefreshGifHeader *)header;
{
//    NSArray *idleImages = [NSArray arrayWithObject:[UIImage imageNamed:@"搜索0001"]];
//    [header setImages:idleImages forState:MJRefreshStateIdle];
    [header setTitle:@"下拉更新" forState:MJRefreshStateIdle];
    [header setTitle:@"松开更新" forState:MJRefreshStatePulling];
    [header setTitle:@"更新中 ..." forState:MJRefreshStateRefreshing];
//    header.stateLabel.font = [UIFont systemFontOfSize:15];
//    NSArray*imgs = @[[UIImage imageNamed:@"搜索0001"],[UIImage imageNamed:@"搜索0002"],[UIImage imageNamed:@"搜索0003"],[UIImage imageNamed:@"搜索0004"],[UIImage imageNamed:@"搜索0005"],[UIImage imageNamed:@"搜索0006"],[UIImage imageNamed:@"搜索0007"],[UIImage imageNamed:@"搜索0008"],[UIImage imageNamed:@"搜索0009"],[UIImage imageNamed:@"搜索0010"],[UIImage imageNamed:@"搜索0011"]];
//    [header setImages:imgs forState:MJRefreshStateRefreshing];
//    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
//    header.stateLabel.textColor = COLOR(187, 187, 187, 1);
//    header.lastUpdatedTimeLabel.textColor =  COLOR(187, 187, 187, 1);
//    header.lastUpdatedTimeLabel.hidden = YES;
}
+(void)MJfoodDH:(MJRefreshAutoGifFooter *)footer
{
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载 ..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"已经到底了" forState:MJRefreshStateNoMoreData];
    
}
+(void)MJDefaultfoodDH:(MJRefreshFooter *)footer
{
    
//    [footer setTitle:@"" forState:MJRefreshStateIdle];
//    [footer setTitle:@"正在加载 ..." forState:MJRefreshStateRefreshing];
//    [footer setTitle:@"已经到底了" forState:MJRefreshStateNoMoreData];
    
}


//时间转化
+(NSString *)compareCurrentTime:(NSString*)string
{
    //    NSString *dateStr=@"2012-5-4 4:34:23";
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];
    
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* compareDate = [inputFormatter dateFromString:string];
    
    NSDate *data1 = [NSDate date];
    NSString *ss = [inputFormatter stringFromDate:data1];
    NSDate *date = [inputFormatter dateFromString:ss];
    
    NSTimeInterval date1Ti = [compareDate timeIntervalSinceReferenceDate];
    NSTimeInterval date2Ti = [date timeIntervalSinceReferenceDate];
    //    int hours = [comps hour];//时间差
    long timeInterval = date2Ti - date1Ti;
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}
//上传图片
+(void)promptViewmessage:(NSString *)message VC:(UIViewController*)VC sureBlock:(void(^)())sureBlock
{
    UIAlertController * alert= [HTTPURL showOneAlertWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert sureTitle:@"确定" sureBlock:^{
        if (sureBlock) {
            sureBlock();
        }
    }];
    [VC presentViewController:alert animated:YES completion:nil];
    
}


@end
